﻿package ru.indee.ui.detectForm 
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import ru.indee.geom.line;
	
	/**
	 * надо сделать кеширование данных
	 * и прочие вкусняшки, но потом когда свариться
	 * @author Raidnew
	 */
	public class objectForDetect 
	{

		private var arrayPoints:Array;
		
		private var lastPoint:Point;
		
		public function objectForDetect(newArrPoints:Array = null) 
		{
			if (newArrPoints == null) {
				arrayPoints = new Array();
			}else {
				arrayPoints = newArrPoints.slice();
			}
		}
		
		/**
		 * add point in form path
		 * @param	newPoint
		 * @return count points in form
		 */
		public function addPoint(newPoint:Point):Number {
			if ((lastPoint == null) || (Point.distance(lastPoint, newPoint) > settingLogic.minimalDistanceBeforePoints)) {
				lastPoint = newPoint.clone();
				arrayPoints.push(newPoint);
			}
			return arrayPoints.length;
		}
		
		public function clearForm():void {
			arrayPoints = new Array();
		}
		
		public function getPointsArray():Array {
			return arrayPoints.slice();
		}
		
		public function setPointArray(newArr:Array):void {
			arrayPoints = newArr.slice();
		}
		
		/**
		 * Возвращает угол между стартовой и финишной линями
		 * @return
		 */
		public function getAngleStartFinishLine():Number {
			var curangle1:Number = line.angle(arrayPoints[0], arrayPoints[1]);
			var curangle2:Number = line.angle(arrayPoints[arrayPoints.length - 2], arrayPoints[arrayPoints.length - 1]);
			var deltaAngle:Number = curangle1 - curangle2;
			if (deltaAngle > 180) {
				deltaAngle = -(360 - Math.abs(deltaAngle));
			}else if (deltaAngle < -180){
				deltaAngle = (360 - Math.abs(deltaAngle));
			}
			return Math.abs(deltaAngle);
		}
		
		/**
		 * возвращает массив углов данной формы
		 * @param	withSign со знаками, или по модулю
		 * @return
		 */
		public function getAngleArray(withSign:Boolean = true):Array {
			var retArray:Array = new Array();
			var curangle1:Number = 0;
			var curangle2:Number = 0;
			var deltaAngle:Number = 0;
			//retArray.push(NaN);
			for (var i:Number = 1; i < (arrayPoints.length - 1); i++) {
				curangle1 = line.angle(arrayPoints[i], arrayPoints[i + 1]);
				curangle2 = line.angle(arrayPoints[i - 1], arrayPoints[i]);
				deltaAngle = curangle1 - curangle2;
				if (deltaAngle > 180) {
					deltaAngle = -(360 - Math.abs(deltaAngle));
				}else if (deltaAngle < -180){
					deltaAngle = (360 - Math.abs(deltaAngle));
				}
				if (withSign) retArray.push(deltaAngle);
				else retArray.push(Math.abs(deltaAngle));
				
			}
			//retArray.push(NaN);
			return retArray;
		}
		
		/**
		 * возвращает общую длинну линии
		 * @return
		 */
		public function getFullLengthForm():Number {
			var summ:Number = 0;
			for (var i:Number = 0; i < (arrayPoints.length - 1); i++) {
				summ += Point.distance(arrayPoints[i],arrayPoints[i+1]);
			}
			return summ;
		}
		
		/**
		 * возвращает максимальный линейный размер формы
		 * @return
		 */
		public function getMaxSizeForm():Number {
			var maxSize:Number = 0;
			for (var i:Number = 0; i < arrayPoints.length; i++) {
				for (var j:Number = 0; j < arrayPoints.length; j++) {
					if (Point.distance(arrayPoints[i], arrayPoints[j]) > maxSize) {
						maxSize = Point.distance(arrayPoints[i], arrayPoints[j]);
					}
				}
			}
			return maxSize;
		}
		
		/**
		 * показывает форму, если будет ругань на нехватку класов то грожнуть эту функцию ф топку
		 * @param	keyShow
		 */
		public function testPath(keyShow:Boolean):void {
			if(keyShow){
				var sprite:Sprite = new Sprite();
				
				var angleArr:Array = getAngleArray();
				
				sprite.graphics.lineStyle(2, 0xFF00FF);
				sprite.graphics.drawCircle(arrayPoints[0].x, arrayPoints[0].y, 10);
				
				for (var i:Number = 1; i < (arrayPoints.length); i++) {
					
					sprite.graphics.lineStyle(1, 0x00FF00);
					sprite.graphics.moveTo(arrayPoints[i-1].x,arrayPoints[i-1].y);
					sprite.graphics.lineTo(arrayPoints[i].x, arrayPoints[i].y);
					
					sprite.graphics.lineStyle(1, 0xAAAAAA);
					sprite.graphics.moveTo(arrayPoints[i].x-10,arrayPoints[i].y);
					sprite.graphics.lineTo(arrayPoints[i].x+10,arrayPoints[i].y);
					sprite.graphics.moveTo(arrayPoints[i].x,arrayPoints[i].y-10);
					sprite.graphics.lineTo(arrayPoints[i].x, arrayPoints[i].y + 10);
					
					if((i - 1) < angleArr.length){
						debug.outGraphicsText(arrayPoints[i], angleArr[i - 1].toString().slice(0,5));
					}
				}
				
				sprite.graphics.lineStyle(2, 0x00FFFF);
				sprite.graphics.moveTo(arrayPoints[(arrayPoints.length - 1)].x-10,arrayPoints[(arrayPoints.length - 1)].y-10);
				sprite.graphics.lineTo(arrayPoints[(arrayPoints.length - 1)].x+10,arrayPoints[(arrayPoints.length - 1)].y+10);
				sprite.graphics.moveTo(arrayPoints[(arrayPoints.length - 1)].x+10,arrayPoints[(arrayPoints.length - 1)].y-10);
				sprite.graphics.lineTo(arrayPoints[(arrayPoints.length - 1)].x-10, arrayPoints[(arrayPoints.length - 1)].y + 10);
				
				debug.addChild(sprite);
			}else {
				debug.removeAllChilds();
			}
		}
		
		/**
		 * возращет срдений угол данноый формы
		 * @return
		 */
		public function getAverageAngle():Number {
			
			var angleArray:Array = getAngleArray(false);// detectLogic.getArrayAngle(tempArray);
			
			var summAngle:Number = 0;
			
			for (var i:Number = 0; i < angleArray.length; i++) {
				summAngle += Math.abs(angleArray[i]);
			}
			
			var averageAngle:Number = summAngle / angleArray.length;
			
			return averageAngle;
		}		

	}
	
}